import 'package:flutter/material.dart';
import 'package:get/get.dart';

/// 应用基础对话框
class AppDialog extends StatelessWidget {
  final String? title;
  final Widget content;
  final List<Widget>? actions;
  final EdgeInsetsGeometry? titlePadding;
  final EdgeInsetsGeometry? contentPadding;
  final EdgeInsetsGeometry? actionsPadding;
  final double? elevation;
  final ShapeBorder? shape;

  const AppDialog({
    Key? key,
    this.title,
    required this.content,
    this.actions,
    this.titlePadding = const EdgeInsets.fromLTRB(24.0, 20.0, 24.0, 0.0),
    this.contentPadding = const EdgeInsets.fromLTRB(24.0, 20.0, 24.0, 24.0),
    this.actionsPadding = const EdgeInsets.symmetric(horizontal: 8.0, vertical: 8.0),
    this.elevation,
    this.shape,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      title: title != null ? Text(title!) : null,
      titlePadding: titlePadding,
      content: content,
      contentPadding: contentPadding,
      actions: actions,
      actionsPadding: actionsPadding,
      elevation: elevation,
      shape: shape ?? RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(12.0),
      ),
    );
  }
  
  /// 显示确认对话框
  static Future<bool?> showConfirmDialog({
    required BuildContext context,
    String? title,
    required String message,
    String confirmText = '确定',
    String cancelText = '取消',
  }) async {
    return await Get.dialog<bool>(
      AppDialog(
        title: title ?? '确认操作',
        content: Text(message),
        actions: [
          TextButton(
            onPressed: () => Get.back(result: false),
            child: Text(cancelText),
          ),
          ElevatedButton(
            onPressed: () => Get.back(result: true),
            child: Text(confirmText),
          ),
        ],
      ),
      barrierDismissible: false, // 通常确认对话框不应点击外部关闭
    );
  }

  /// 显示信息对话框
  static Future<void> showInfoDialog({
    required BuildContext context,
    String? title,
    required String message,
    String confirmText = '知道了',
  }) async {
    return await Get.dialog<void>(
      AppDialog(
        title: title ?? '提示',
        content: Text(message),
        actions: [
          ElevatedButton(
            onPressed: () => Get.back(),
            child: Text(confirmText),
          ),
        ],
      ),
    );
  }
  
  /// 显示错误对话框
  static Future<void> showErrorDialog({
    required BuildContext context,
    String? title,
    required String message,
    String confirmText = '确定',
  }) async {
    return await Get.dialog<void>(
      AppDialog(
        title: title ?? '发生错误',
        content: Row(
          children: [
            Icon(Icons.error_outline, color: Theme.of(context).colorScheme.error, size: 28),
            const SizedBox(width: 16),
            Expanded(child: Text(message)),
          ],
        ),
        actions: [
          ElevatedButton(
            onPressed: () => Get.back(),
            child: Text(confirmText),
            style: ElevatedButton.styleFrom(
              backgroundColor: Theme.of(context).colorScheme.error,
            ),
          ),
        ],
      ),
    );
  }

  /// 显示加载对话框
  static void showLoadingDialog(BuildContext context, {String message = '正在加载...'}) {
    Get.dialog(
      AppDialog(
        content: Row(
          children: [
            const CircularProgressIndicator(),
            const SizedBox(width: 24),
            Text(message),
          ],
        ),
      ),
      barrierDismissible: false, // 加载中不可关闭
    );
  }
  
  /// 关闭加载对话框
  static void hideLoadingDialog() {
    if (Get.isDialogOpen ?? false) {
      Get.back();
    }
  }
} 